package com.yanqu.road.server.cmd.activity.twins.pvp;

import com.yanqu.road.entity.activity.twins.config.TwinsConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.TwinsProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.twins.TwinsModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.twins.TwinsMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_TWINS_PVP_ENTER_MAIN, desc = "")
public class TwinsPvpEnterMainCmd extends AbstractCommand {

    TwinsProto.TwinsPvpEnterMainReqMsg reqMsg;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        reqMsg = TwinsProto.TwinsPvpEnterMainReqMsg.parseFrom(packet.getBytes());
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        TwinsConfig config = TwinsMgr.getConfig();
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        int checkGroup = player.getModule(TwinsModule.class).checkGroup();
        if (checkGroup != 0) {
            ret = checkGroup;
            return;
        }
        int activityId = config.getActivityInfo().getActivityId();
        int groupId = GameUnionActivityGroupMgr.getGroupId(activityId, player.getUserId());

        player.sendUnionActivityGroupPacket(CrossProtocol.C2_CROSS_TWINS_PVP_ENTER_MAIN, reqMsg.toBuilder(),activityId,groupId);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret == 0) {
            return;
        }
        TwinsProto.TwinsPvpEnterMainResMsg.Builder respMsg = TwinsProto.TwinsPvpEnterMainResMsg.newBuilder();
        respMsg.setRet(ret);
        player.sendPacket(ClientProtocol.U_TWINS_PVP_ENTER_MAIN, respMsg);
    }

}
